package org.csu.myPetStore.web.servlet.ajax;

import org.csu.myPetStore.service.AccountService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class CheckUserNameExistServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("Enter Check");
        AccountService accountService = new AccountService();

        String username = (String) req.getParameter("username");
        boolean result = accountService.isUserNameExist(username);

        resp.setContentType("text/plain");
        PrintWriter out = resp.getWriter();

        if(result){
            out.print("Exist");
        }else{
            out.print("Not Exist");
        }

        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
